package com.project.XXXX.api;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.project.XXXX.inter.tbl_dictMapper;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.aldjob.controllerImpl.EmployeePtMangaeController;
import com.aldjob.controllerImpl.EmployerPtMangaeController;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.project.XXXX.controllerInterface.IJobManager;
import com.project.XXXX.identification.tool.ValidateTool;
import com.project.XXXX.inter.tbl_divisionMapper;
import com.project.XXXX.inter.tbl_employeeinfoMapper;
import com.project.XXXX.inter.tbl_employerinfoMapper;
import com.project.XXXX.inter.tbl_jobenrollMapper;
import com.project.XXXX.inter.tbl_ptjobMapper;
import com.project.XXXX.inter.tbl_sysuserMapper;
import com.project.XXXX.inter.tbl_workplaceMapper;
import com.project.XXXX.inter.tbl_worktimeMapper;
import com.project.XXXX.model.tbl_employeeinfo;
import com.project.XXXX.model.tbl_jobenroll;
import com.project.XXXX.model.tbl_ptjob;
import com.project.XXXX.model.tbl_ptjobWithBLOBs;
import com.project.XXXX.model.tbl_sysuser;
import com.project.XXXX.model.tbl_workplace;
import com.project.XXXX.model.tbl_worktime;
import com.project.XXXX.stringmodel.EEinfoAndjobString;
import com.project.XXXX.stringmodel.PtJobAddress;
import com.project.XXXX.stringmodel.PtJobForShown;
import com.project.XXXX.stringmodel.PtJobTime;
import com.project.XXXX.tools.ProjectTools;

@Controller
public class batching implements IJobManager {
	@Resource
	private tbl_ptjobMapper tbl_ptjobMapper;
	@Resource
	private tbl_sysuserMapper tbl_sysuserMapper;
	@Resource
	private tbl_worktimeMapper tbl_worktimeMapper;
	@Resource
	private tbl_dictMapper tbl_dictMapper;
	@Resource
	private tbl_jobenrollMapper tbl_jobenrollMapper;
	@Resource
	private tbl_divisionMapper tbl_divisionMapper;
	@Resource
	private tbl_workplaceMapper tbl_workplaceMapper;
	@Resource
	private tbl_employeeinfoMapper tbl_employeeinfoMapper;
	@Resource
	private tbl_employerinfoMapper tbl_employerinfoMapper;

	EmployerPtMangaeController uc;
	boolean first;
	public class batch{
		private String id;
		private String status;
		private String error_description;
		public batch(String id, String status, String error_description) {
			super();
			this.id = id;
			this.status = status;
			this.error_description = error_description;
		}
		
	}
	void prepare(HttpServletResponse response) {
		if (first) {
			// System.out.println("is first");
			uc = new EmployerPtMangaeController(tbl_divisionMapper, tbl_employeeinfoMapper, tbl_jobenrollMapper, tbl_worktimeMapper, tbl_ptjobMapper, tbl_dictMapper, tbl_workplaceMapper, tbl_employerinfoMapper);
			first = false;
		}
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
	}

	HttpSession getAppSession(String vaidatecodeid) {
		SessionContext sessionContext = SessionContext.getInstance();
		return sessionContext.getSession(vaidatecodeid);
	}

	public batching() {
		first = true;
	}

	@RequestMapping(value = "/api/employerManage/batching/before/start")
	public void batchstart(@RequestParam("id") String ptid,@RequestParam("access_token") String sessonid,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		prepare(response);
		//System.out.println(ptid);
		String [] ids=ptid.split(",");
		List<batch> l = new ArrayList<batch>();
		HttpSession sess = getAppSession(sessonid);
		for(String i:ids){
			//System.out.println(i);
			int status = uc.changestatus(28, Integer.parseInt(i), sess);//进行中
			switch (status) {
			case 200:
				l.add(new batch(i,"success",null));
				break;
			case 403:
				l.add(new batch(i,"failure","please log in"));
				break;
			case 409:
				l.add(new batch(i,"failure","not your pt"));
				break;
			}
		}
		ProjectTools.ResponseOut(response, new Gson().toJson(l));
	}

	@RequestMapping(value = "/api/employerManage/batching/before/delete")
	public void batchdelete(@RequestParam("id") String ptid,@RequestParam("access_token") String sessonid,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		prepare(response);
		//System.out.println(ptid);
		String [] ids=ptid.split(",");
		List<batch> l = new ArrayList<batch>();
		HttpSession sess = getAppSession(sessonid);
		for(String i:ids){
			//System.out.println(i);
			int status = uc.changestatus(41, Integer.parseInt(i), sess);//删除
			switch (status) {
			case 200:
				l.add(new batch(i,"success",null));
				break;
			case 403:
				l.add(new batch(i,"failure","please log in"));
				break;
			case 409:
				l.add(new batch(i,"failure","not your pt"));
				break;
			}
		}
		ProjectTools.ResponseOut(response, new Gson().toJson(l));
	}
	@RequestMapping(value = "/api/employerManage/batching/ongoing/end")
	public void batchend(@RequestParam("id") String ptid,@RequestParam("access_token") String sessonid,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		prepare(response);
		//System.out.println(ptid);
		String [] ids=ptid.split(",");
		List<batch> l = new ArrayList<batch>();
		HttpSession sess = getAppSession(sessonid);
		for(String i:ids){
			//System.out.println(i);
			int status = uc.changestatus(40, Integer.parseInt(i), sess);//结束报名
			switch (status) {
			case 200:
				l.add(new batch(i,"success",null));
				break;
			case 403:
				l.add(new batch(i,"failure","please log in"));
				break;
			case 409:
				l.add(new batch(i,"failure","not your pt"));
				break;
			}
		}
		ProjectTools.ResponseOut(response, new Gson().toJson(l));
	}
	@RequestMapping(value = "/api/employerManage/batching/ongoing/pass")
	public void batchpass(@RequestParam("id") String ptid,@RequestParam("access_token") String sessonid,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		prepare(response);
		//System.out.println(ptid);
		String [] ids=ptid.split(",");
		List<batch> l = new ArrayList<batch>();
		HttpSession sess = getAppSession(sessonid);
		for(String i:ids){
			//System.out.println(i);
			int status = uc.changeJobStatus(31, Integer.parseInt(i), sess);//审核通过
			switch (status) {
			case 200:
				l.add(new batch(i,"success",null));
				break;
			case 403:
				l.add(new batch(i,"failure","please log in"));
				break;
			case 409:
				l.add(new batch(i,"failure","not your pt"));
				break;
			}
		}
		ProjectTools.ResponseOut(response, new Gson().toJson(l));
	}
	@RequestMapping(value = "/api/employerManage/batching/ongoing/fail")
	public void batchfail(@RequestParam("id") String ptid,@RequestParam("access_token") String sessonid,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		prepare(response);
		//System.out.println(ptid);
		String [] ids=ptid.split(",");
		List<batch> l = new ArrayList<batch>();
		HttpSession sess = getAppSession(sessonid);
		for(String i:ids){
			//System.out.println(i);
			int status = uc.changeJobStatus(32, Integer.parseInt(i), sess);//审核不通过
			switch (status) {
			case 200:
				l.add(new batch(i,"success",null));
				break;
			case 403:
				l.add(new batch(i,"failure","please log in"));
				break;
			case 409:
				l.add(new batch(i,"failure","not your pt"));
				break;
			}
		}
		ProjectTools.ResponseOut(response, new Gson().toJson(l));
	}

	@RequestMapping(value = "/api/employerManage/batching/after/delete")
	public void batchdeletea(@RequestParam("id") String ptid,@RequestParam("access_token") String sessonid,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		prepare(response);
		//System.out.println(ptid);
		String [] ids=ptid.split(",");
		List<batch> l = new ArrayList<batch>();
		HttpSession sess = getAppSession(sessonid);
		for(String i:ids){
			//System.out.println(i);
			int status = uc.changestatus(41, Integer.parseInt(i), sess);//删除
			switch (status) {
			case 200:
				l.add(new batch(i,"success",null));
				break;
			case 403:
				l.add(new batch(i,"failure","please log in"));
				break;
			case 409:
				l.add(new batch(i,"failure","not your pt"));
				break;
			}
		}
		ProjectTools.ResponseOut(response, new Gson().toJson(l));
	}
}
